//
//  KYSettingItemCell.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/9/21.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYSettingItemCell.h"
#import "KYSettingItem.h"
#import "KYSettingArrowItem.h"
#import "KYSettingSwitchItem.h"
#import "KYSettingCheckItem.h"
#import "KYSettingLabelItem.h"
#import "KYBadgeButton.h"

@interface KYSettingItemCell()
/**
 *  引用tableView
 */
@property (weak, nonatomic) UITableView *tableView;
/**
 *  子标题
 */
@property (weak, nonatomic) UILabel *subtitleLabel;
/**
 *  左边详细的描述
 */
@property (nonatomic, weak) UILabel *detailLeftLabel;
/**
 *  右边详细的描述
 */
@property (nonatomic, weak) UILabel *detailRightLabel;
/**
 *  用户头像
 */
@property (nonatomic, weak) UIImageView *iconView;
/**
 *  箭头(这里为什么用stong终于明白了不能让创建完了就被销毁)
 */
@property (strong, nonatomic) UIImageView *arrowView;
/**
 *  打钩
 */
@property (strong, nonatomic) UIImageView *checkView;
/**
 *  开关
 */
@property (strong, nonatomic) UISwitch *switchView;
/**
 *  标签
 */
@property (nonatomic, strong) UILabel *markLb;
/**
 *  错误提醒
 */
@property (nonatomic, strong) UIButton *messageBtn;
/**
 *  提醒数字
 */
@property (nonatomic, strong) KYBadgeButton *badgeButton;
@end

@implementation KYSettingItemCell

- (KYBadgeButton *)badgeButton
{
    if (_badgeButton == nil) {
        _badgeButton = [[KYBadgeButton alloc] init];
    }
    return _badgeButton;
}

- (UIImageView *)arrowView
{
    if (_arrowView == nil) {
        _arrowView = [[UIImageView alloc] initWithImage:[UIImage imageWithName:@"common_icon_arrow"]];
    }
    return _arrowView;
}

- (UIImageView *)checkView
{
    if (_checkView == nil) {
        _checkView = [[UIImageView alloc] initWithImage:[UIImage imageWithName:@"common_icon_checkmark"]];
    }
    return _checkView;
}

- (UISwitch *)switchView
{
    if (_switchView == nil) {
        _switchView = [[UISwitch alloc] init];
        [_switchView addTarget:self action:@selector(switchStateChange) forControlEvents:UIControlEventValueChanged];
    }
    return _switchView;
}

/**
 *  开关状态改变
 */
- (void)switchStateChange
{
    [KYUserDefaults setBool:self.switchView.isOn forKey:self.item.title];
    [KYUserDefaults synchronize];
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"itemCell";
    KYSettingItemCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[KYSettingItemCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
        cell.tableView = tableView;
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // 子标题
        UILabel *subtitleLabel = [[UILabel alloc] init];
        subtitleLabel.backgroundColor = [UIColor clearColor];
        subtitleLabel.textColor = [UIColor lightGrayColor];
        subtitleLabel.highlightedTextColor = subtitleLabel.textColor;
        subtitleLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:subtitleLabel];
        self.subtitleLabel = subtitleLabel;
        
        // 左边的描述
        UILabel *detailLeftLabel = [[UILabel alloc] init];
        detailLeftLabel.backgroundColor = [UIColor clearColor];
        detailLeftLabel.textColor = [UIColor lightGrayColor];
        detailLeftLabel.highlightedTextColor = subtitleLabel.textColor;
        detailLeftLabel.font = [UIFont systemFontOfSize:13];
        detailLeftLabel.numberOfLines = 0;
        [self.contentView addSubview:detailLeftLabel];
        self.detailLeftLabel = detailLeftLabel;
        
        // 右边的描述
        UILabel *detailRightLabel = [[UILabel alloc] init];
        detailRightLabel.backgroundColor = [UIColor clearColor];
        detailRightLabel.textColor = [UIColor lightGrayColor];
        detailRightLabel.highlightedTextColor = subtitleLabel.textColor;
        detailRightLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:detailRightLabel];
        self.detailRightLabel = detailRightLabel;
        
        // 个人头像
        UIImageView *iconView = [[UIImageView alloc] init];
        iconView.layer.masksToBounds = YES;
        [self.contentView addSubview:iconView];
        self.iconView = iconView;
        
        // 标题
        self.textLabel.backgroundColor = [UIColor clearColor];
        self.textLabel.textColor = [UIColor blackColor];
        self.textLabel.highlightedTextColor = self.textLabel.textColor;
        self.textLabel.font = [UIFont systemFontOfSize:14.5];
        
        // 最右边的详情文字
        self.detailTextLabel.backgroundColor = [UIColor clearColor];
        self.detailTextLabel.textColor = [UIColor blackColor];
        self.detailTextLabel.highlightedTextColor = self.detailTextLabel.textColor;
        self.detailTextLabel.font = [UIFont systemFontOfSize:13];
        
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)setItem:(KYSettingItem *)item
{
    _item = item;
    
    // 1.设置数据
    [self setUpData];
    
    // 2.设置右边的控件
    [self setUpRightView];
}

/**
 *  设置数据
 */
- (void)setUpData
{
    // 1.图标
    if (self.item.icon) {
        self.imageView.image = [UIImage imageWithName:self.item.icon];
    }
    
    // 2.标题&标题颜色
    self.textLabel.text = self.item.title;
    if (self.item.titleColor) {
        self.textLabel.textColor = self.item.titleColor;
    }
    
    // 3.子标题
    if (self.item.subtitle) {
        self.subtitleLabel.hidden = NO;
        self.subtitleLabel.text = self.item.subtitle;
    } else {
        self.subtitleLabel.hidden = YES;
    }
    
    // 4.左边的描述
    if (self.item.detailLeftLabel) {
        self.detailLeftLabel.hidden = NO;
        self.detailLeftLabel.text = self.item.detailLeftLabel;
    } else {
        self.detailLeftLabel.hidden = YES;
    }
    
    // 5.右边的描述
    if (self.item.detailRightLabel) {
        self.detailRightLabel.hidden = NO;
        self.detailRightLabel.text = self.item.detailRightLabel;
    } else {
        self.detailRightLabel.hidden = YES;
    }
    
    // 6.右边的文字
    self.detailTextLabel.text = self.item.detailLabel;
    
    // 7.个人头像
    if (self.item.imageViewStr) {
        if ([self.item.imageViewStr containsString:@"http://"]) {
            [self.iconView setImageWithURL:[NSURL URLWithString:self.item.imageViewStr]];
        } else {
            self.iconView.image = [UIImage base64StrToUIImage:self.item.imageViewStr];
        }
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 设置子标题
    CGSize subtitleSize = [self.item.subtitle sizeWithAttributes:@{NSFontAttributeName : self.subtitleLabel.font}];
    CGFloat subtitleW = subtitleSize.width;
    CGFloat subtitleH = subtitleSize.height;
    CGFloat subtitleX = CGRectGetMaxX(self.textLabel.frame) + 5;
    CGFloat subtitleY = (self.contentView.frame.size.height - subtitleH) * 0.5;
    self.subtitleLabel.frame = CGRectMake(subtitleX, subtitleY, subtitleW, subtitleH);
    
    // 设置左边详情
    CGSize detailLeftLabelSize = [self.item.detailLeftLabel sizeWithAttributes:@{NSFontAttributeName : self.detailLeftLabel.font}];
    CGFloat detailLeftLabelW = detailLeftLabelSize.width;
    CGFloat detailLeftLabelH = detailLeftLabelSize.height;
    CGFloat detailLeftLabelX = 15;
    CGFloat detailLeftLabelY = CGRectGetMaxY(self.textLabel.frame) + 5;
    self.detailLeftLabel.frame = CGRectMake(detailLeftLabelX, detailLeftLabelY, detailLeftLabelW, detailLeftLabelH);
    
    // 设置右边详情
    CGSize detailRightLabelSize = [self.item.detailRightLabel sizeWithAttributes:@{NSFontAttributeName : self.detailRightLabel.font}];
    CGFloat detailRightLabelW = detailRightLabelSize.width;
    CGFloat detailRightLabelH = detailRightLabelSize.height;
    CGFloat detailRightLabelX = self.contentView.frame.size.width - detailLeftLabelX - detailRightLabelSize.width;
    CGFloat detailRightLabelY = CGRectGetMaxY(self.textLabel.frame) + 5;
    self.detailRightLabel.frame = CGRectMake(detailRightLabelX, detailRightLabelY, detailRightLabelW, detailRightLabelH);
    
    // 个人头像
    CGFloat iconViewY = 3;
    CGFloat iconViewH = self.contentView.frame.size.height - 2 * iconViewY;
    CGFloat iconViewW = iconViewH;
    CGFloat iconViewX = self.contentView.frame.size.width - iconViewW - 10;
    self.iconView.frame = CGRectMake(iconViewX, iconViewY, iconViewW, iconViewH);
    self.iconView.layer.cornerRadius = iconViewW / 2;
}

/**
 *  设置右边的控件
 */
- (void)setUpRightView
{
    if (self.item.badgeValue) { // 右边显示数字
        self.badgeButton.value = self.item.badgeValue;
        self.accessoryView = self.badgeButton; // 用点语法才会去访问getter方法
    } else if ([self.item isKindOfClass:[KYSettingLabelItem class]]) { // 右边是文字
        self.accessoryView = nil;
        KYSettingLabelItem *labelItem = (KYSettingLabelItem *)self.item;
        self.detailTextLabel.text = labelItem.text;
    } else if ([self.item isKindOfClass:[KYSettingSwitchItem class]]) { // 右边是开关
        KYSettingSwitchItem *switchItem = (KYSettingSwitchItem *)self.item;
        self.switchView.on = switchItem.isOn;
        self.accessoryView = self.switchView;
    } else if ([self.item isKindOfClass:[KYSettingArrowItem class]]) { // 右边是箭头
        self.accessoryView = self.arrowView;
    } else if ([self.item isKindOfClass:[KYSettingCheckItem class]]) { // 右边是打钩
        KYSettingCheckItem *checkItem = (KYSettingCheckItem *)self.item;
        self.accessoryView = checkItem.isChecked ? self.checkView : nil;
    } else if ([self.item isKindOfClass:nil]) { // 弹出日期
        
    } else { // 右边没有东西\是解决cell的循环利用
        self.accessoryView = nil;
    }
}

- (void)setFrame:(CGRect)frame
{
    frame.origin.x = 6;
    frame.size.width -= 2 * 6;
    
    [super setFrame:frame];
}

- (void)setIndexPath:(NSIndexPath *)indexPath
{
    _indexPath = indexPath;
    
    NSInteger totalRows = [self.tableView numberOfRowsInSection:indexPath.section];
    NSString *bgName = nil;
    NSString *selectedBgName = nil;
    if (totalRows == 1) { // 这组就1行
        bgName = @"common_card_background";
        selectedBgName = @"common_card_background_highlighted";
    } else if (indexPath.row == 0) { // 首行
        bgName = @"common_card_top_background";
        selectedBgName = @"common_card_top_background_highlighted";
    } else if (indexPath.row == totalRows - 1) { // 尾行
        bgName = @"common_card_bottom_background";
        selectedBgName = @"common_card_bottom_background_highlighted";
    } else { // 中行
        bgName = @"common_card_middle_background";
        selectedBgName = @"common_card_middle_background_highlighted";
    }
    self.bg.image = [UIImage resizedImageWithName:bgName];
    self.selectedBg.image = [UIImage resizedImageWithName:selectedBgName];
}

@end
